<?php defined('BASEPATH') OR exit('No direct script access allowed');

class expenses extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		$this->load->helper(array('form', 'url'));
       	$this->load->model('expenses_model');
         $this->load->library('datatables');	
        $this->load->library('upload');		 
	}
public function all_expenses()
{
$meta['page_title'] = 'ALL Engineer Expenses';
$this->load->view('common/header',$meta);
$this->load->view('expenses/all_expenses');
$this->load->view('common/footer');

}	
public function get_all()
{
echo $this->expenses_model->get_all();

}
public function get_all_nonwarranty()
{
echo $this->expenses_model->get_all_nonwarranty();

}
public function assigned_engineer()
{
$meta['page_title'] = 'ALL Assigned Engineer';
$this->load->view('common/header',$meta);
$this->load->view('expenses/all_assigned');
$this->load->view('common/footer');

}	
public function getall_assigned()
{
echo $this->expenses_model->get_all_assigned();
}	
public function getall_assigned_nonwarranty()
{
echo $this->expenses_model->get_all_assigned_nonwarranty();
}	
public function change_engineer($id)
{
 $data['engineer']=$this->expenses_model->get_engineer();
 $data['case_detail']=$this->expenses_model->get_case_detail($id)->row();
$data['admin']=$this->expenses_model->chk_admin_assigned($data['case_detail']->engineer_id)->row();
$meta['page_title'] = 'Change Engineer';
$this->load->view('common/header',$meta);
$this->load->view('expenses/change_engineer',$data);
$this->load->view('common/footer');
}
public function change_engineer_nonwarranty($id)
{
 $data['engineer']=$this->expenses_model->get_engineer();
 $data['case_detail']=$this->expenses_model->get_case_detail_non($id)->row();
$data['admin']=$this->expenses_model->chk_admin_assigned($data['case_detail']->engineer_id)->row();
$meta['page_title'] = 'Change Engineer';
$this->load->view('common/header',$meta);
$this->load->view('expenses/change_engineer_non',$data);
$this->load->view('common/footer');
}	
public function engineer_change()
{
if(!empty($_REQUEST['admin']))
{
$id_engineer=$this->session->userdata['user_id'];
} else{

$id_engineer=$_REQUEST['id_engineer'];
}
$case_no=$_REQUEST['case_no'];
$case_id=$this->input->post('case_id');
$this->expenses_model->update_case($case_id,$case_no,$id_engineer);
$this->expenses_model->update_diagnosis($case_id,$case_no,$id_engineer);
$this->expenses_model->update_repair($case_id,$case_no,$id_engineer);
redirect('admin/expenses/assigned_engineer');	 
}
public function engineer_change_non()
{
if(!empty($_REQUEST['admin']))
{
$id_engineer=$this->session->userdata['user_id'];
} else{

$id_engineer=$_REQUEST['id_engineer'];
}
$case_no=$_REQUEST['case_no'];
$case_id=$this->input->post('case_id');
$this->expenses_model->update_nonwarranty_case($case_id,$case_no,$id_engineer);
$this->expenses_model->update_nonwarranty_diagnosis($case_id,$case_no,$id_engineer);
$this->expenses_model->update_nonwarranty_repair($case_id,$case_no,$id_engineer);
redirect('admin/expenses/assigned_engineer');	 
}
public function report()
{
$meta['page_title'] = 'Report';
$this->load->view('common/header',$meta);
$this->load->view('expenses/report');
$this->load->view('common/footer');

}
public function prepare_report()
{
$to_date=$_REQUEST['to_date'];
$from_date=$_REQUEST['from_date'];
$type=$_REQUEST['type'];
//1 for warranty...2 for non warranty
if($type==1)
{
$this->data['reports']=$this->expenses_model->warranty_report($from_date,$to_date);
$this->load->view('expenses/report_detail',$this->data);
}
else{
$this->data['reports']=$this->expenses_model->nonwarranty_report($from_date,$to_date);
$this->load->view('expenses/report_detail',$this->data);
}
}
function all_worksheet($year_id = null,$month_id = null,$date_id = null){

		$meta['page_title'] = "All Worksheet";
		$this->data['year_id'] = $year_id;
		$this->data['month_id'] = $month_id;
		$this->data['date_id'] = $date_id;
		$this->data['users']=$this->expenses_model->get_users();
		$this->load->view('common/header', $meta);
		$this->load->view('expenses/worksheet',$this->data);
		$this->load->view('common/footer');	
	}
function worksheet($year_id = null,$month_id = null,$date_id = null){
        if($month_id < 10){ $month_id='0'.$month_id; } else { $month_id=$month_id;}
		$date='20'.$year_id.'/'.$month_id.'/'.$date_id;
		$this->data['users']=$this->expenses_model->get_users();
		$this->data['date']=$date;
		$this->data['year_id'] = $year_id;
		$this->data['month_id'] = $month_id;
		$this->data['date_id'] = $date_id;
		$meta['page_title'] = "All Worksheet";
		$this->load->view('common/header', $meta);
		$this->load->view('expenses/worksheet_date',$this->data);
		$this->load->view('common/footer');	
	}
public function upload_scan()
{
$case_id=$_REQUEST['case_id'];
$case_no=$_REQUEST['case_no'];
$received_date=$_REQUEST['received_date'];
 $config['upload_path'] = './scan/warranty';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size']	= '20480';
		$config['max_width']  = '1024';
		$config['max_height']  = '1024';
		$this->load->library('upload',$config);
	    $new_img =  $_FILES['user_file']['name'];
		$tmpName  = $_FILES['user_file']['tmp_name'];
		if($_REQUEST['scan']=1){
		$insert=array('case_id'=>$case_id,
		              'case_no'=>$case_no,
					  'file_name' =>$new_img,
					  'date'      =>$received_date,);
		$this->expenses_model->insert_scan($insert);
		$this->expenses_model->update_case_deliver($case_id);
        $data =array( 'upload_data' => move_uploaded_file($tmpName,"./scan/warranty/$new_img"));		
} else{ }
redirect('admin/repair/view_completed_repair');	
}
public function upload_nonwarranty_scan()
{
$case_id=$_REQUEST['case_id'];
$case_no=$_REQUEST['case_no'];
$received_date=$_REQUEST['received_date'];
 $config['upload_path'] = './scan/nonwarranty';
		$config['allowed_types'] = 'gif|jpg|png|jpeg|doc|txt|pdf|zip|rar';
		$config['max_size']	= '20480';
		$config['max_width']  = '1024';
		$config['max_height']  = '1024';
		$this->load->library('upload',$config);
	    $new_img =  $_FILES['user_file']['name'];
		$tmpName  = $_FILES['user_file']['tmp_name'];
		if($_REQUEST['scan']=1){
		$insert=array('case_id'=>$case_id,
		              'case_no'=>$case_no,
					  'file_name' =>$new_img,
					  'date'      =>$received_date,);
		$this->expenses_model->insert_nonwarranty_scan($insert);
		$this->expenses_model->update_nonwarranty_case_deliver($case_id);
        $data =array( 'upload_data' => move_uploaded_file($tmpName,"./scan/nonwarranty/$new_img"));		
} else{ }
redirect('admin/repair/view_completed_repair');	
}
public function deliver_customer()
{
$meta['page_title'] = 'Customer Delivered ';
$this->load->view('common/header',$meta);
$this->load->view('print/delivered_customer');
$this->load->view('common/footer');
}
public function get_completed()
{
echo $this->expenses_model->get_completed();
}
public function nonwarranty_completed()
{
echo $this->expenses_model->nonwarranty_completed();
}
public function view_scan_copy($id)
{
$data['scan']=$this->expenses_model->get_customer_scan($id)->row();
$meta['page_title'] = 'Customer Scan Copy ';
$this->load->view('common/header',$meta);
$this->load->view('print/customer_scan',$data);
$this->load->view('common/footer');
}
public function view_scan_copy_nonwarranty($id)
{
$data['scan']=$this->expenses_model->get_customer_scan_nonwarranty($id)->row();
$meta['page_title'] = 'Customer Scan Copy ';
$this->load->view('common/header',$meta);
$this->load->view('print/customer_scan_nonwarranty',$data);
$this->load->view('common/footer');
}






	
}
?>	
	